/*
 * SplashPresenter.java
 * business
 *
 * Created by ChenTao on 2017/2/28.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.splash;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.data.model.CommonModel;
import com.yidiandao.business.data.model.ICommonModel;
import com.yidiandao.business.data.model.IConfigModel;
import com.yidiandao.business.data.response.SplashResponse;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.PriceUtils;
import com.yidiandao.business.utils.TimeUtils;
import com.yidiandao.business.utils.UIUtils;

/**
 * splash界面
 */
public class SplashPresenter implements SplashContract.Presenter {

    private SplashContract.View mView;

    private IConfigModel mConfigModel;

    private Runnable mDelayTask;
    private final ICommonModel mCommonModel;

    /**
     * 请求成功后  间隔5秒自动跳转
     */
    private int timeSecond = 5;
    private Runnable mDelayEnterTask;

    public SplashPresenter(SplashContract.View view, IConfigModel configModel) {
        mView = view;
        mConfigModel = configModel;
        mCommonModel = new CommonModel();
        mView.setPresenter(this);
    }

    @Override
    public void start() {
        mConfigModel.setOpenAppCount();
        mConfigModel.getConfig(mView.getPagerTag());
        mConfigModel.getBottomBarUpdate(mView.getPagerTag());
        if (PriceUtils.isLogin())
            getSplashAD();
    }

    @Override
    public void onFragmentResume() {
        UIUtils.removeCallbacks(mDelayTask);
        UIUtils.postDelayed(mDelayTask, Common.SPLASH_TIME);
    }

    @Override
    public void initDelayTask() {
        mDelayTask =
                new Runnable() {
                    @Override
                    public void run() {
                        //判断是否显示引导界面
                        startActivity();
                    }
                };
        mDelayEnterTask = new Runnable() {
            @Override
            public void run() {
                timeSecond--;
                mView.setTimeTip(timeSecond);
                setTimeTip();
            }
        };
    }

    private void getSplashAD() {
        final int preSeconds = TimeUtils.getCurSeconds();
        mCommonModel.getSplashAD(mView.getPagerTag(), new ICommonModel.onAccessNetListener<SplashResponse.DataBean.PictureInfoBean>() {
            @Override
            public void onSuccess(SplashResponse.DataBean.PictureInfoBean pictureInfoBean) {
                int second = TimeUtils.getCurSeconds() - preSeconds;
                if (second <= 2 && null != pictureInfoBean) {
                    mView.setSplashData(pictureInfoBean);
                    UIUtils.removeCallbacks(mDelayTask);
                    setTimeTip();
                }
            }

            @Override
            public void onFailed() {

            }

            @Override
            public void onFinal() {

            }
        });
    }

    private void setTimeTip() {
        if (timeSecond != 0)
            UIUtils.postDelayed(mDelayEnterTask, Common.COUNT_DOWN_TIME);
        else
            startActivity();
    }

    @Override
    public void startActivity() {
        if (mConfigModel.isGuideShowed()) {
            //判断是否进入选择地址界面
            if (!mConfigModel.getCurrentCity().equals(PreferenceUtils.STRING_DEFAULT)) {
                //判断是否登录
                if (PriceUtils.isLogin()) {
                    mView.toMainActivity();
                } else {
                    mView.toLoginActivity();
                }
            } else {
                mView.toLocationActivity();
            }
        } else {
            mView.toGuideActivity();
        }

        mView.finishActivity();
    }


    @Override
    public void onFragmentPause() {
        UIUtils.removeCallbacks(mDelayTask);
        UIUtils.removeCallbacks(mDelayEnterTask);
    }
}
